<?php 
namespace Mage\ApiBundle\Soap;

class MagConnect {


	private $sessionId;
	
	private $client;
	
	private $responseSoap;
	
	private $responseHtml;
	
	
    public function __construct(){

    	$this->client = new \SoapClient('http://kidlou.local/fr/index.php/api/soap/?wsdl');
    	$this->sessionId = $this->client->login('symfony2', '123456'); // TODO : change login and pwd if necessary
    }
	

//     public function getCategories(){
//     	$this->responseSoap = $this->client->call($this->sessionId, 'catalog_category.tree');
// 		$this->responseHtml = '<div class="col-md-6">' . $this->getNavigation($this->responseSoap['children']) . '</div>	';
// 		return $this;
//     }
    
    public function getCategories(){
    	$this->responseSoap = $this->client->call($this->sessionId, 'catalog_category.tree');
    	$this->responseHtml = $this->getNavigation($this->responseSoap['children']);
    	return $this;
    }
    
    public function getSoapResponse(){
    	return $this->responseSoap;
    }
    
    public function getHtmlResponse(){
    	return $this->responseHtml;
    }
    
    

    
    private function getNavigation($result){
    	$str_navigation = '';
    	foreach( $result as $data ){
    		if(count($data['children']) > 0){
    			$arr_data[$data['category_id']] = $this->getLevel($data['level']) . $data['name'];
    			$arr_temp = $this->getNavigation($data['children']);
    			foreach($arr_temp as $key_temp => $data_temp){
    				$arr_data[$key_temp] = $data_temp;
    			}
    		} else {
    			$arr_data[$data['category_id']] = $this->getLevel($data['level']) . $data['name'];
    		}
    	}
    	return $arr_data;
    }
    
    
    private function getLevel($level){
    	$str = '';
    	for($i=1; $i<$level; $i++){
    		$str .= '-';
    	}
    	return $str;
    }
    
    

}